﻿/* Write a program that enters file name along with its full file path 
 * (e.g. C:\WINDOWS\win.ini), reads its contents and prints it on the console. 
 * Find in MSDN how to use System.IO.File.ReadAllText(…). 
 * Be sure to catch all possible exceptions and print user-friendly error messages.
*/
using System;
using System.IO;
using System.Security;

namespace _3.ReadFileContents
{
    class Read_Print_FileContents
    {
        static void Main()
        {
            Console.Write("Enter path: ");
            string path_to_file = @Console.ReadLine();
            string readFile = null;
            try
            {
                readFile = File.ReadAllText(path_to_file);
            }
            catch (FileNotFoundException)
            {
                throw new FileNotFoundException("The file could not be found in the specified path/directory");
            }
            catch (OverflowException)
            {
                throw new OverflowException("Out of memory. May be the provided file is too big?");
            }
            catch (PathTooLongException)
            {
                throw new PathTooLongException("Error: the provided path is too long");
            }
            catch (ArgumentNullException anull)
            {
                throw new DriveNotFoundException("Invalid argument" + anull.Message);
            }
            catch (ArgumentException ae)
            {
                throw new DriveNotFoundException("Invalid argument" + ae.Message);
            }
            catch (DirectoryNotFoundException dnf)
            {
                throw new DirectoryNotFoundException("The specified directory could not be found " + dnf.Message);
            }
            catch (IOException io)
            {
                throw new IOException("IO Exception " + io.Message);
            }
            catch (UnauthorizedAccessException accessEx)
            {
                throw new UnauthorizedAccessException(string.Format("You have no permissions to perform this operation\r\n{0}", accessEx));
            }
            catch (NotSupportedException NotSupportedEx)
            {
                throw new NotSupportedException(string.Format("Unsupported action\r\nDebug trace: {0}", NotSupportedEx.Message));
            }
            catch (SecurityException se)
            {
                throw new SecurityException("The perform operation could not be performed for security reasons. Please check whether you have the requried privileges. " + se.Message);
            }
            finally
            {
                Console.WriteLine(readFile);
            }
        }
    }
}
